package com.yanqu.road.server.manager.activity.peakseacraft;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.peakseacraft.*;
import com.yanqu.road.entity.activity.peakseacraft.config.PeakSeacraftActivityConfig;
import com.yanqu.road.entity.activity.seacraft.config.*;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eUnionSyncType;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroup;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.logic.pb.UnionBasePb;
import com.yanqu.road.pb.activity.PeakSeacraftProto;
import com.yanqu.road.pb.activity.SeacraftSouthSeaProto;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.pb.worldmessage.WorldMessageProto;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempCrossMgr;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description 巅峰海上争霸
 */
public class PeakSeacraftMgr extends TempCrossMgr {

    public static Map<Integer, PeakSeacraftActivityGroup> activityGroupMap = new ConcurrentHashMap<>();

    public static Random random = new Random();

    @Override
    public boolean init() throws Exception {
        reload();
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
//        PeakSeacraftConfigMgr.reloadConfig();
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        getLogger().info("start reload PeakSeacraft activity  start");
        String nowTimeStr = DateHelper.getCurrentDateTimeString();
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.PeakSeacraftActivity.getValue());
        Map<Integer, ActivityInfo> tempActivityInfoMap = ActivityBussiness.getOpenCrossRankActivityInfoMap(activityTypeList, nowTimeStr);
        for (ActivityInfo activityInfo : tempActivityInfoMap.values()) {
            Map<Integer, UnionActivityGroup> activityGroupMap = Cross2UnionActivityGroupMgr.getGroupMap(activityInfo.getActivityId());
            if (null != activityGroupMap) {
                for (UnionActivityGroup activityGroup : activityGroupMap.values()) {
                    if (activityGroup.getCrossServerId() == CrossBaseServer.getInstance().getServerId()) {
                        reloadPeakSeacraftActivity(activityInfo, activityGroup.getGroupId());
                    }
                }
            }
        }
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    @Override
    public boolean save() {
        for (PeakSeacraftActivityGroup item : activityGroupMap.values()) {
            item.save();
        }
        return true;
    }

    @Override
    public boolean removeExpireData() {
        int currentSecond = DateHelper.getCurrentSecond();
        List<Integer> removeActivity = new ArrayList<>();
        synchronized (activityGroupMap) {
            for (PeakSeacraftActivityGroup item : activityGroupMap.values()) {
                PeakSeacraftActivityConfig config = PeakSeacraftConfigMgr.getConfigMap().get(item.getActivityId());
                if (config == null) {
                    removeActivity.add(item.getActivityId());
                } else if (currentSecond >= config.getActivityInfo().getEndShowTime() + 600) {
                    removeActivity.add(item.getActivityId());
                }
            }

            if (!removeActivity.isEmpty()) {
                removeActivity.forEach(activityId -> {
                    activityGroupMap.remove(activityId);
                    PeakSeacraftConfigMgr.removeConfig(activityId);
                });
            }
        }
        return true;
    }

    /**
     * @param activityInfo
     * @param groupId
     * @return
     */
    public static synchronized void reloadPeakSeacraftActivity(ActivityInfo activityInfo, int groupId) {
        int activityId = activityInfo.getActivityId();
        PeakSeacraftActivityConfig config = PeakSeacraftConfigMgr.reloadConfig(activityInfo);
        if (config == null) {
            return;
        }
        PeakSeacraftActivity peakSeacraftActivity = getPeakSeacraftActivity(activityId, groupId);
        if (peakSeacraftActivity != null) {
            peakSeacraftActivity.updateConfig(config);
        }
    }

    /**
     * @param activityId
     * @return
     */
    public static PeakSeacraftActivity getPeakSeacraftActivity(int activityId, int unionGroup) {
        if (unionGroup == UnionActivityGroupHelper.NO_GROUP_ID) {
            return null;
        }
        PeakSeacraftActivityGroup activityGroup = activityGroupMap.get(activityId);
        if (activityGroup == null) {
            synchronized (activityGroupMap) {
                activityGroup = activityGroupMap.get(activityId);
                if (activityGroup == null) {
                    activityGroup = new PeakSeacraftActivityGroup(activityId);
                    activityGroupMap.put(activityId, activityGroup);
                }
            }
        }
        return activityGroup.getActivity(unionGroup);
    }

    /**
     * 同步锁定联盟的联盟成员给跨服
     *
     * @param activityId
     * @param unionList
     * @param groupId
     */
    public static void synPromotionUnion(int activityId, List<PeakSeacraftProto.PeakSeacraftUnionInfo> unionList, List<PeakSeacraftProto.PeakSeacraftLockUserInfo> userTeamInfoList, int groupId) {
        PeakSeacraftActivity southSeaActivity = getPeakSeacraftActivity(activityId, groupId);
        synchronized (southSeaActivity) {
            southSeaActivity.synPromotionUnion(unionList);
            southSeaActivity.synUserTeamInfo(userTeamInfoList);
        }
    }

    public static int getCurRound(int activityId) {
        PeakSeacraftActivityConfig activityConfig = PeakSeacraftConfigMgr.getConfigMap().get(activityId);
        if (activityConfig == null) {
            return 0;
        }
        ActivityInfo activityInfo = ActivityMgr.getActivityInfo(activityId);
        if (activityInfo == null) {
            return 0;
        }
        return activityConfig.getCurRound();
    }

    /**
     * 锁定晋级联盟 在这里分组
     */
    public static synchronized void lockPromotionUnionTask() {
        for (PeakSeacraftActivityGroup item : activityGroupMap.values()) {
            // 当前定时器执行到轮次
            item.lockPromotionUnionTask();
        }
    }

    /**
     * 第一阶段一开始就 向区服索要 联盟数据 和联盟成员的门客数据
     */
    public static synchronized void synUnionInfoTask() {
        for (PeakSeacraftActivityGroup item : activityGroupMap.values()) {
            item.synUnionInfoTask();
        }
    }

    /**
     * 索要晋级的联盟数据
     *
     * @param activityId
     * @param groupId
     */
    public static void getPromotionUnion(int activityId, long serverId, int groupId) {
        PeakSeacraftActivity southSeaActivity = getPeakSeacraftActivity(activityId, groupId);
        if (southSeaActivity == null) {
            return;
        }
        if (southSeaActivity.getCurRound() <= 0) {
            return;
        }
        PeakSeacraftProto.PeakSeacraftSynPromotionUnionCrossToGameMsg.Builder crossToGameMsg = southSeaActivity.getPromotionUnion();
        if (crossToGameMsg.getUnionIdList().size() > 0) {
            MessageHelper.sendPacket(serverId, 0, YanQuMessageUtils.buildMessage(GameProtocol.S_PEAK_SEACRAFT_LOCK_JOIN_UNION_FROM_CROSS, crossToGameMsg.setUnionGroup(groupId)));
        }
    }

    /**
     * 抵达任务
     */
    public static void arrivalTask() {
        for (PeakSeacraftActivityGroup item : activityGroupMap.values()) {
            item.arrivalTask();
        }
    }


    /**
     * 获取玩家数据
     */
    public static void getUserData(int activityId, long userId, long serverId, PeakSeacraftProto.PeakSeacraftGetUserDataGameToCrossMsg msg) {
        PeakSeacraftProto.PeakSeacraftRespMsg.Builder builder = PeakSeacraftProto.PeakSeacraftRespMsg.newBuilder();
        int unionGroup = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        PeakSeacraftActivity southSeaActivity = getPeakSeacraftActivity(activityId, unionGroup);
        if (southSeaActivity == null) {
            return;
        }

        PeakSeacraftProto.PeakSeacraftUserInfo.Builder userInfoMsg = southSeaActivity.getPeakSeacraftUserInfo(userId, msg.getUnionId(), serverId);
        userInfoMsg.setFloor(msg.getFloor());
        userInfoMsg.setSilverDonateNum(msg.getSilverDonateNum());
        userInfoMsg.setWorship(msg.getWorship());
        builder.setUserInfo(userInfoMsg);
        builder.addAllUnionIdList(southSeaActivity.getAllUnionIdList());
        builder.addAllResultList(southSeaActivity.getPeakSeacraftResultList(msg.getUnionId()));
        PeakSeacraftProto.PeakSeacraftShipInfo.Builder shipInfo = southSeaActivity.getUnionShip(userId);
        if (shipInfo != null) {
            builder.setUnionShip(shipInfo);
        }

        MessageHelper.sendPacket(serverId, userId, YanQuMessageUtils.buildMessage(ClientProtocol.U_PEAK_SEACRAFT_GET_USER_DATA, builder));
    }

    /**
     * 查看捐献的船只
     *
     * @param activityId
     * @param userId
     * @param unionId
     * @return
     */
    public static PeakSeacraftProto.PeakSeacraftGetDonationShipRespMsg.Builder getDonationShip(int activityId, long userId, String unionId) {
        PeakSeacraftProto.PeakSeacraftGetDonationShipRespMsg.Builder builder = PeakSeacraftProto.PeakSeacraftGetDonationShipRespMsg.newBuilder();
        int unionGroup = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        PeakSeacraftActivity southSeaActivity = getPeakSeacraftActivity(activityId, unionGroup);
        if (southSeaActivity == null) {
            builder.setRet(GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_NO_OPEN);
            return builder;
        }
        return southSeaActivity.getUnionDonationShipList(userId, unionId);
    }


    /**
     * 捐献
     */
    public static void donationShip(long userId, long serverId, int activityId, String unionId, int donateExp, int type, String cost) {
        int unionGroup = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        PeakSeacraftActivity southSeaActivity = getPeakSeacraftActivity(activityId, unionGroup);
        if (southSeaActivity == null) {
            return;
        }
        southSeaActivity.donationShip(userId, serverId, unionId, donateExp, type, cost);
    }

    /**
     * 查看 联盟的捐献 详情
     *
     * @param activityId
     * @return
     */
    public static PeakSeacraftProto.PeakSeacraftGetDonationShipDetailRespMsg.Builder getDonationShipDetail(int activityId, long userId, String unionId) {
        int unionGroup = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        PeakSeacraftProto.PeakSeacraftGetDonationShipDetailRespMsg.Builder builder = PeakSeacraftProto.PeakSeacraftGetDonationShipDetailRespMsg.newBuilder();
        PeakSeacraftActivity southSeaActivity = getPeakSeacraftActivity(activityId, unionGroup);
        if (southSeaActivity == null) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        return southSeaActivity.getDonationShipDetail(userId, unionId);
    }

    /**
     * 进入南海丝路
     */
    public static PeakSeacraftProto.PeakSeacraftEnterRespMsg.Builder southSeaEnter(int activityId, long userId, long refreshTime,String unionId) {
        int unionGroup = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        PeakSeacraftProto.PeakSeacraftEnterRespMsg.Builder builder = PeakSeacraftProto.PeakSeacraftEnterRespMsg.newBuilder();
        PeakSeacraftActivity southSeaActivity = getPeakSeacraftActivity(activityId, unionGroup);
        if (southSeaActivity == null) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        return southSeaActivity.southSeaEnter(userId, refreshTime, unionId);
    }

    /**
     * 查看组内船只列表
     */
    public static PeakSeacraftProto.PeakSeacraftGetGroupDetailRespMsg.Builder getAreaGroupShipList(int activityId, String unionId, long userId, PeakSeacraftProto.PeakSeacraftGetGroupDetailReqMsg reqMsg) {
        int unionGroup = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        PeakSeacraftProto.PeakSeacraftGetGroupDetailRespMsg.Builder builder = PeakSeacraftProto.PeakSeacraftGetGroupDetailRespMsg.newBuilder();
        PeakSeacraftActivity southSeaActivity = getPeakSeacraftActivity(activityId, unionGroup);
        if (southSeaActivity == null) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }

        return southSeaActivity.getAreaGroupShipList(unionId, reqMsg.getArea(), reqMsg.getGroup(), userId, reqMsg.getRefreshTime());
    }

    /**
     * 查看船只详情
     */
    public static PeakSeacraftProto.PeakSeacraftGetShipRecordRespMsg.Builder getShipRecord(int activityId, String unionId, int type) {
        int unionGroup = Cross2UnionActivityGroupMgr.getGroupId(activityId, unionId);
        PeakSeacraftProto.PeakSeacraftGetShipRecordRespMsg.Builder builder = PeakSeacraftProto.PeakSeacraftGetShipRecordRespMsg.newBuilder();
        PeakSeacraftActivity southSeaActivity = getPeakSeacraftActivity(activityId, unionGroup);
        if (southSeaActivity == null) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }

        return southSeaActivity.getShipRecord(unionId, type);
    }

    /**
     * @param activityId
     * @param type          操作类型 0 进攻 1突袭 2使用道具进攻 3修复 5 使用船桨
     * @param userId
     * @param serverId
     * @param ownUnionId    玩家的unionId
     * @param targetUnionId 要攻击的联盟id  type 0 2 才需要传
     * @param dispatchType  派遣的小队类型  type = 0,1 才需要传
     * @param titleId
     * @param count         使用道具的数量  type = 2 3 4 才有
     * @return
     */
    public static PeakSeacraftProto.PeakSeacraftActionCrossToGameMsg.Builder action(int activityId, int type, long userId, long serverId, String ownUnionId, String targetUnionId, int dispatchType, int titleId, int count, long preReduceCount,int propId) {
        int unionGroup = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        PeakSeacraftProto.PeakSeacraftActionCrossToGameMsg.Builder builder = PeakSeacraftProto.PeakSeacraftActionCrossToGameMsg.newBuilder();
        builder.setType(type);
        builder.setCount(preReduceCount);
        builder.setPropId(propId);
        builder.setActivityId(activityId);
        builder.setUnionGroup(unionGroup);
        PeakSeacraftProto.PeakSeacraftActionRespMsg.Builder stcMsg = PeakSeacraftProto.PeakSeacraftActionRespMsg.newBuilder();
        PeakSeacraftActivity southSeaActivity = getPeakSeacraftActivity(activityId, unionGroup);
        if (southSeaActivity == null) {
            stcMsg.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            builder.setStcMsg(stcMsg);
            return builder;
        }

        stcMsg = southSeaActivity.action(type, userId, serverId, ownUnionId, targetUnionId, titleId, dispatchType, count, preReduceCount, propId);

        if (stcMsg.getRet() == 0 && (type == 0 || type == 1)) {
            // 下发小队的门客
            PeakSeacraftCrossUserInfo userInfo = southSeaActivity.getUserInfoMap().get(userId);
            if (userInfo != null && userInfo.getChallengeData() != null) {
                for (PeakSeacraftLockPatronsInfo item : userInfo.getChallengeData().getPatronsInfoList()) {
                    builder.addPatronsId(item.getId());
                }
            }
        }
        builder.setStcMsg(stcMsg);
        return builder;
    }

    /**
     * 购买buff
     */
    public static PeakSeacraftProto.PeakSeacraftBuyBuffCrossToGameMsg.Builder buyBuff(int activityId, long userId, int preReduceCount, int buffId) {
        int unionGroup = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        PeakSeacraftProto.PeakSeacraftBuyBuffCrossToGameMsg.Builder builder = PeakSeacraftProto.PeakSeacraftBuyBuffCrossToGameMsg.newBuilder();
        builder.setPreReduceCount(preReduceCount);
        PeakSeacraftProto.PeakSeacraftBuyBuffRespMsg.Builder stcMsg = PeakSeacraftProto.PeakSeacraftBuyBuffRespMsg.newBuilder();
        PeakSeacraftActivity southSeaActivity = getPeakSeacraftActivity(activityId, unionGroup);
        if (southSeaActivity == null) {
            stcMsg.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            builder.setStcMsg(stcMsg);
            return builder;
        }

        stcMsg = southSeaActivity.buyBuff(userId, buffId, preReduceCount);

        builder.setStcMsg(stcMsg);
        return builder;
    }

    /**
     * 挑战
     *
     * @param activityId
     * @param userId
     * @param targetId
     */
    public static PeakSeacraftProto.PeakSeacraftChallengeCrossToGameMsg.Builder challenge(int activityId, long userId, long targetId, int type, int blood, int buffId, long goldNum) {
        int unionGroup = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        PeakSeacraftProto.PeakSeacraftChallengeCrossToGameMsg.Builder crossToGameMsg = PeakSeacraftProto.PeakSeacraftChallengeCrossToGameMsg.newBuilder();
        crossToGameMsg.setGoldNum(goldNum);
        crossToGameMsg.setType(type);
        PeakSeacraftProto.PeakSeacraftChallengeRespMsg.Builder stcMsg = PeakSeacraftProto.PeakSeacraftChallengeRespMsg.newBuilder();
        PeakSeacraftActivity southSeaActivity = getPeakSeacraftActivity(activityId, unionGroup);
        if (southSeaActivity == null) {
            stcMsg.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            crossToGameMsg.setStcMsg(stcMsg);
            return crossToGameMsg;
        }
        stcMsg = southSeaActivity.challenge(userId, targetId, type, blood, buffId, goldNum);
        crossToGameMsg.setStcMsg(stcMsg);
        return crossToGameMsg;
    }


    /**
     * 获取挑战数据
     *
     * @param activityId
     * @param userId
     */
    public static PeakSeacraftProto.PeakSeacraftGetChallengeRespMsg.Builder getChallenge(int activityId, long userId) {
        int unionGroup = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        PeakSeacraftProto.PeakSeacraftGetChallengeRespMsg.Builder builder = PeakSeacraftProto.PeakSeacraftGetChallengeRespMsg.newBuilder();
        PeakSeacraftActivity southSeaActivity = getPeakSeacraftActivity(activityId, unionGroup);
        if (southSeaActivity == null) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        return southSeaActivity.getChallengeInfo(userId);
    }

    /**
     * 领取连胜奖励
     *
     * @param activityId
     * @param userId
     */
    public static PeakSeacraftProto.PeakSeacraftReceiveWinStreakRewardRespMsg.Builder receiveWinStreakReward(int activityId, long userId) {
        int unionGroup = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        PeakSeacraftProto.PeakSeacraftReceiveWinStreakRewardRespMsg.Builder builder = PeakSeacraftProto.PeakSeacraftReceiveWinStreakRewardRespMsg.newBuilder();
        PeakSeacraftActivity southSeaActivity = getPeakSeacraftActivity(activityId, unionGroup);
        if (southSeaActivity == null) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        synchronized (southSeaActivity) {
            return southSeaActivity.receiveWinStreakReward(userId);
        }
    }

    /**
     * 领取竞猜奖励
     *
     * @param activityId
     * @param userId
     */
    public static PeakSeacraftProto.PeakSeacraftReceiveGuessRewardCrossToGameMsg.Builder receiveGuessReward(int activityId, long userId) {
        int unionGroup = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        PeakSeacraftProto.PeakSeacraftReceiveGuessRewardCrossToGameMsg.Builder builder = PeakSeacraftProto.PeakSeacraftReceiveGuessRewardCrossToGameMsg.newBuilder();
        PeakSeacraftActivity southSeaActivity = getPeakSeacraftActivity(activityId, unionGroup);
        if (southSeaActivity == null) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        synchronized (southSeaActivity) {
            return southSeaActivity.receiveGuessReward(userId);
        }
    }

    /**
     * 进入竞猜
     *
     * @param activityId
     * @param groupId
     */
    public static PeakSeacraftProto.PeakSeacraftEnterGuessRespMsg.Builder enterGuess(int activityId, int groupId) {
        PeakSeacraftProto.PeakSeacraftEnterGuessRespMsg.Builder builder = PeakSeacraftProto.PeakSeacraftEnterGuessRespMsg.newBuilder();
        PeakSeacraftActivity southSeaActivity = getPeakSeacraftActivity(activityId, groupId);
        if (southSeaActivity == null) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }

        return southSeaActivity.enterGuess();
    }

    /**
     * 竞猜
     *
     * @param userId
     * @param unionList
     */
    public static PeakSeacraftProto.PeakSeacraftGuessRespMsg.Builder guess(int activityId, long userId, long serverId, String ownUnionId, List<String> unionList) {
        int unionGroup = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        PeakSeacraftProto.PeakSeacraftGuessRespMsg.Builder builder = PeakSeacraftProto.PeakSeacraftGuessRespMsg.newBuilder();
        PeakSeacraftActivity southSeaActivity = getPeakSeacraftActivity(activityId, unionGroup);
        if (southSeaActivity == null) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        if(DateHelper.getCurrentSecond() >= getRoundStartTime(activityId, southSeaActivity.getConfig().getMaxRoundCount())){
            // 不能竞猜该轮数据
            builder.setRet(GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_THE_ROUND_GUESS_NO_OPEN);
            return builder;
        }
        synchronized (southSeaActivity) {
            return southSeaActivity.guess(userId, ownUnionId, serverId, unionList);
        }
    }

    /**
     * 查看竞猜成功的玩家
     *
     * @param activityId
     * @param round
     * @param guessNum
     * @param num
     * @param pageNum
     * @param groupId
     */
    public static PeakSeacraftProto.PeakSeacraftGetGuessSuccessUserRespMsg.Builder getGuessSuccessUser(int activityId, int round, int guessNum, int num, int pageNum, int groupId) {
        PeakSeacraftProto.PeakSeacraftGetGuessSuccessUserRespMsg.Builder builder = PeakSeacraftProto.PeakSeacraftGetGuessSuccessUserRespMsg.newBuilder();
        PeakSeacraftActivity southSeaActivity = getPeakSeacraftActivity(activityId, groupId);
        if (southSeaActivity == null) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }

        return southSeaActivity.getGuessSuccessUser(round, guessNum);
    }

    /**
     * 查看联盟排名
     *
     * @param activityId
     * @param userId
     * @param unionId
     * @return
     */
    public static PeakSeacraftProto.PeakSeacraftGetUnionRankRespMsg.Builder getUnionRank(int activityId, long userId, String unionId) {
        int unionGroup = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        PeakSeacraftProto.PeakSeacraftGetUnionRankRespMsg.Builder builder = PeakSeacraftProto.PeakSeacraftGetUnionRankRespMsg.newBuilder();
        PeakSeacraftActivity southSeaActivity = getPeakSeacraftActivity(activityId, unionGroup);
        if (southSeaActivity == null) {
            builder.setRet(GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_NO_OPEN);
            return builder;
        }

        return southSeaActivity.getUnionRank(userId, unionId);
    }

    /**
     * 查看联盟排名列表 组内
     *
     * @param activityId
     * @param userId
     */
    public static PeakSeacraftProto.PeakSeacraftGetUnionRankListRespMsg.Builder getUnionRankList(int activityId, long userId, String unionId, int area, int group) {
        int unionGroup = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        PeakSeacraftProto.PeakSeacraftGetUnionRankListRespMsg.Builder builder = PeakSeacraftProto.PeakSeacraftGetUnionRankListRespMsg.newBuilder();
        PeakSeacraftActivity southSeaActivity = getPeakSeacraftActivity(activityId, unionGroup);
        if (southSeaActivity == null) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        return southSeaActivity.getGroupUnionRankList(userId, unionId, area, group);
    }


    /**
     * 领取排行榜奖励
     *
     * @param activityId
     * @param userId
     */
    public static SeacraftSouthSeaProto.SeacraftSouthSeaReceiveUnionRankCrossToGameMsg.Builder receiveUnionRank(int activityId, long userId) {
        int unionGroup = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        SeacraftSouthSeaProto.SeacraftSouthSeaReceiveUnionRankCrossToGameMsg.Builder builder = SeacraftSouthSeaProto.SeacraftSouthSeaReceiveUnionRankCrossToGameMsg.newBuilder();
        PeakSeacraftActivity southSeaActivity = getPeakSeacraftActivity(activityId, unionGroup);
        if (southSeaActivity == null) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        synchronized (southSeaActivity) {
            return southSeaActivity.receiveUnionRank(userId);
        }
    }

    /**
     * 查看抵达信息
     *
     * @param activityId
     * @param unionId
     */
    public static PeakSeacraftProto.PeakSeacraftGetArrivalInfoRespMsg.Builder getArrivalInfo(int activityId, String unionId) {
        int unionGroup = Cross2UnionActivityGroupMgr.getGroupId(activityId, unionId);
        PeakSeacraftProto.PeakSeacraftGetArrivalInfoRespMsg.Builder builder = PeakSeacraftProto.PeakSeacraftGetArrivalInfoRespMsg.newBuilder();
        PeakSeacraftActivity southSeaActivity = getPeakSeacraftActivity(activityId, unionGroup);
        if (southSeaActivity == null) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }

        return southSeaActivity.getArrivalInfo(unionId);
    }

    /**
     * @param activityId
     * @param unionId
     * @return
     */
    public static PeakSeacraftProto.PeakSeacraftGetFixRankRespMsg.Builder getFixRank(int activityId, String unionId) {
        int unionGroup = Cross2UnionActivityGroupMgr.getGroupId(activityId, unionId);
        PeakSeacraftProto.PeakSeacraftGetFixRankRespMsg.Builder builder = PeakSeacraftProto.PeakSeacraftGetFixRankRespMsg.newBuilder();
        PeakSeacraftActivity southSeaActivity = getPeakSeacraftActivity(activityId, unionGroup);
        if (southSeaActivity == null) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }

        return southSeaActivity.getFixRank(unionId);
    }

    /**
     * 查看船只攻防信息
     *
     * @param activityId
     * @param unionId
     * @return
     */
    public static PeakSeacraftProto.PeakSeacraftGetBattleRecordRespMsg.Builder getBattleRecord(int activityId, String unionId, int type) {
        int unionGroup = Cross2UnionActivityGroupMgr.getGroupId(activityId, unionId);
        PeakSeacraftProto.PeakSeacraftGetBattleRecordRespMsg.Builder builder = PeakSeacraftProto.PeakSeacraftGetBattleRecordRespMsg.newBuilder();
        PeakSeacraftActivity southSeaActivity = getPeakSeacraftActivity(activityId, unionGroup);
        if (southSeaActivity == null) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }

        return southSeaActivity.getBattleRecord(unionId, type);
    }


    /**
     * 查看船只成员详细攻防数据
     *
     * @param activityId
     * @param type
     * @param unionId
     * @param sonType
     */
    public static PeakSeacraftProto.PeakSeacraftGetMemberBattleDetailRespMsg.Builder getMemberBattleDetail(int activityId, int type, String unionId, int sonType, String targetId) {
        int unionGroup = Cross2UnionActivityGroupMgr.getGroupId(activityId, unionId);
        PeakSeacraftProto.PeakSeacraftGetMemberBattleDetailRespMsg.Builder builder = PeakSeacraftProto.PeakSeacraftGetMemberBattleDetailRespMsg.newBuilder();
        PeakSeacraftActivity southSeaActivity = getPeakSeacraftActivity(activityId, unionGroup);
        if (southSeaActivity == null) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }

        return southSeaActivity.getMemberBattleDetail(type, unionId, sonType, targetId);
    }

    /**
     * 查看联盟加速榜
     *
     * @param activityId
     * @param unionId
     * @return
     */
    public static PeakSeacraftProto.PeakSeacraftGetSpeedUpRecordRespMsg.Builder getSpeedUpRecord(int activityId, String unionId) {
        int unionGroup = Cross2UnionActivityGroupMgr.getGroupId(activityId, unionId);
        PeakSeacraftProto.PeakSeacraftGetSpeedUpRecordRespMsg.Builder builder = PeakSeacraftProto.PeakSeacraftGetSpeedUpRecordRespMsg.newBuilder();
        PeakSeacraftActivity southSeaActivity = getPeakSeacraftActivity(activityId, unionGroup);
        if (southSeaActivity == null) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        List<PeakSeacraftProto.PeakSeacraftSpeedUpRecordInfo> recordInfoList = southSeaActivity.getSpeedUpRecord(unionId);
        builder.addAllRecordList(recordInfoList);
        builder.setRet(0);
        return builder;
    }

    /**
     * 获取当前船只加速详情
     *
     * @param activityId
     * @param unionId
     * @return
     */
    public static PeakSeacraftProto.PeakSeacraftGetSpeedUpRespMsg.Builder getCurSpeedUpDetail(int activityId, String unionId) {
        int unionGroup = Cross2UnionActivityGroupMgr.getGroupId(activityId, unionId);
        PeakSeacraftProto.PeakSeacraftGetSpeedUpRespMsg.Builder builder = PeakSeacraftProto.PeakSeacraftGetSpeedUpRespMsg.newBuilder();
        PeakSeacraftActivity southSeaActivity = getPeakSeacraftActivity(activityId, unionGroup);
        if (southSeaActivity == null) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        List<PeakSeacraftProto.PeakSeacraftSpeedUpInfo> list = southSeaActivity.getCurSpeedUpDetail(unionId);
        builder.addAllSpeedUpList(list);
        builder.setRet(0);
        return builder;
    }


    /**
     * 玩家 进出联盟, 报名期结束锁定名单后，不能再加入和退出商会, 只更新联盟信息
     *
     * @param type
     * @param activityId
     * @param positionList
     * @param unionId
     */
    public static void changeUnion(int type, int activityId, List<PeakSeacraftProto.PeakSeacraftMemberPositionTemp> positionList, String unionId, UnionProto.UnionBaseTempMsg unionBaseTempMsg) {
        int unionGroup = Cross2UnionActivityGroupMgr.getGroupId(activityId, unionId);
        PeakSeacraftActivity southSeaActivity = getPeakSeacraftActivity(activityId, unionGroup);
        if (southSeaActivity == null) {
            return;
        }
        PeakSeacraftActivityConfig activityConfig = PeakSeacraftConfigMgr.getConfigMap().get(activityId);
        if (activityConfig == null) {
            return;
        }
        synchronized (southSeaActivity) {// 修改联盟信息
            PeakSeacraftUnionInfo unionInfo = southSeaActivity.getUnionInfo(unionId, false);
            if (unionInfo != null) {
                unionInfo.getUnionData().setUnionBaseInfo(UnionBasePb.parseUnionBaseInfo(unionBaseTempMsg));
                unionInfo.getUnionData().setUpdateOption();

            }
            if (type == eUnionSyncType.SetPosition.getValue()) {
                for (PeakSeacraftProto.PeakSeacraftMemberPositionTemp positionTemp : positionList) {
                    PeakSeacraftCrossUserInfo userInfo = southSeaActivity.getUserInfo(positionTemp.getUserId());
                    if (null != userInfo) {
                        userInfo.getBaseData().setUnionPosition(positionTemp.getPosition());
                        if (null != unionInfo) {
                            unionInfo.getUnionData().updateMemberPosition(positionTemp.getUserId(), positionTemp.getPosition());
                        }
                    }
                }
            }
        }
    }

    /**
     * 同步玩家数据
     *
     * @param activityId
     * @param userPatronsInfoList
     * @param groupId
     */
    public static void synUserInfo(int activityId, List<PeakSeacraftProto.PeakSeacraftSynUserPatronsInfo> userPatronsInfoList, int groupId) {
        PeakSeacraftActivity southSeaActivity = getPeakSeacraftActivity(activityId, groupId);
        if (southSeaActivity == null) {
            return;
        }
        synchronized (southSeaActivity) {
            southSeaActivity.synUserInfo(userPatronsInfoList);
        }
    }


    /**
     * 获取船只对应的配置
     *
     * @param unionInfo
     * @return
     */
    public static SeacraftSouthSeaShipConfig getShipConfig(PeakSeacraftUnionInfo unionInfo, boolean isShipExp) {
        SeacraftSouthSeaShipConfig shipConfig = null;
        PeakSeacraftActivityConfig activityConfig = PeakSeacraftConfigMgr.getConfigMap().get(unionInfo.getUnionData().getActivityId());
        int donationExp = unionInfo.getUnionData().getTotalDonationExp();
        if (isShipExp && null != unionInfo.getShipData()){
            donationExp = unionInfo.getShipDonateExp();
        }
        for (SeacraftSouthSeaShipConfig item : activityConfig.getShipList()) {
            if (donationExp < item.getExp()) {
                return item;
            }
            shipConfig = item;
        }
        return shipConfig;
    }

    /**
     * 获取本轮 比赛的开始时间 ms
     *
     * @param activityId
     * @param round
     * @return
     */
    public static long getRoundStartTime(int activityId, int round) {
        PeakSeacraftActivityConfig activityConfig = PeakSeacraftConfigMgr.getConfigMap().get(activityId);
        if (activityConfig == null) {
            return 0;
        }
        ActivityInfo activityInfo = ActivityMgr.getActivityInfo(activityId);
        if (activityInfo == null) {
            return 0;
        }
        if (round > activityConfig.getMaxRoundCount()) {
            return 0;
        }
        Integer[] config = activityConfig.roundTimeList.get(round - 1);
        // 活动开始时间
        return (activityInfo.getBeginTime() + config[0]) * DateHelper.SECOND_MILLIONS;
    }

    /**
     * 获取本轮 比赛的结束时间 ms
     *
     * @param activityId
     * @param round
     * @return
     */
    public static long getRoundEndTime(int activityId, int round) {
        PeakSeacraftActivityConfig activityConfig = PeakSeacraftConfigMgr.getConfigMap().get(activityId);
        ActivityInfo activityInfo = ActivityMgr.getActivityInfo(activityId);
        if (round > activityConfig.getMaxRoundCount()) {
            return 0;
        }
        Integer[] config = activityConfig.roundTimeList.get(round - 1);
        // 活动结束时间
        return (activityInfo.getBeginTime() + config[1]) * DateHelper.SECOND_MILLIONS;
    }

    /**
     * 是否在比赛时间中
     *
     * @return
     */
    public static boolean inRoundTime(int activityId, int round, long now) {
        long roundStartTime = PeakSeacraftMgr.getRoundStartTime(activityId, round);
        if (now < roundStartTime || roundStartTime == 0) {
            // 比赛还未开始
            return false;
        }
        long roundEndTime = PeakSeacraftMgr.getRoundEndTime(activityId, round);
        if (now >= roundEndTime) {
            // 比赛结束
            return false;
        }
        return true;
    }

    public static void parseUnionRankMsgByUserId(int activityId, long userId, StrongholdWarProto.StrongholdActivityUnionRankListRespMsg.Builder msg) {
        int unionGroup = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        PeakSeacraftActivity southSeaActivity = getPeakSeacraftActivity(activityId, unionGroup);
        if (southSeaActivity == null) {
            return;
        }
        PeakSeacraftActivityConfig activityConfig = PeakSeacraftConfigMgr.getConfigMap().get(activityId);
        if (activityConfig == null) {
            return;
        }

        southSeaActivity.parseUnionRankMsgByUserId(userId, msg);
    }

    /**
     * 更新榜单消息
     */
    public static void updateUnionRankMsgTask() {
        for (PeakSeacraftActivityGroup item : activityGroupMap.values()) {
            item.updateUnionRankMsgTask();
        }
    }

    /**
     * 获取商会所有回合结果
     * @param activityId
     * @param unionId
     * @return
     */
    public static PeakSeacraftProto.PeakSeacraftGetUnionRoundResultRespMsg.Builder getUnionRoundResult(int activityId, String unionId){
        int unionGroup = Cross2UnionActivityGroupMgr.getGroupId(activityId, unionId);
        PeakSeacraftProto.PeakSeacraftGetUnionRoundResultRespMsg.Builder builder = PeakSeacraftProto.PeakSeacraftGetUnionRoundResultRespMsg.newBuilder();
        PeakSeacraftActivity southSeaActivity = getPeakSeacraftActivity(activityId, unionGroup);
        if (southSeaActivity == null) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        builder = southSeaActivity.getUnionRoundResult(unionId);
        return builder;
    }

    /**
     * 获取成员所有回合伤害
     * @param activityId
     * @param userId
     * @return
     */
    public static PeakSeacraftProto.PeakSeacraftGetMemberRoundScoreRespMsg.Builder getMemberRoundScore(int activityId, long userId) {
        int unionGroup = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        PeakSeacraftProto.PeakSeacraftGetMemberRoundScoreRespMsg.Builder builder = PeakSeacraftProto.PeakSeacraftGetMemberRoundScoreRespMsg.newBuilder();
        PeakSeacraftActivity southSeaActivity = getPeakSeacraftActivity(activityId, unionGroup);
        if (southSeaActivity == null) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        builder = southSeaActivity.getMemberRoundScore(userId);
        return builder;
    }

    public static void addAreaGroupChatMsg(int activityId, int unionGroup, WorldMessageProto.WorldMessageTempMsg chatMsg) {
        PeakSeacraftActivity activity = getPeakSeacraftActivity(activityId, unionGroup);
        if (activity == null) {
            return;
        }
        int value = Integer.valueOf(chatMsg.getExtraParams());
        int area = value / 100;
        int group = value % 100;
        activity.addGroupChatMsg(area, group, chatMsg);
    }

    public static WorldMessageProto.WorldMessageListMsg.Builder getAreaGroupChatListMsg(int activityId, int unionGroup, int param) {
        WorldMessageProto.WorldMessageListMsg.Builder builder = WorldMessageProto.WorldMessageListMsg.newBuilder();
        PeakSeacraftActivity activity = getPeakSeacraftActivity(activityId, unionGroup);
        if (activity == null) {
            return builder;
        }
        int area = param / 100;
        int group = param % 100;
        return activity.getAreaGroupChatListMsg(area, group);
    }

    public static void clearChatMsg(int activityId){
        PeakSeacraftActivityGroup activityGroup = activityGroupMap.get(activityId);
        if (activityGroup == null) {
            return;
        }
        for (PeakSeacraftActivity activity : activityGroup.getActivityMap().values()) {
            activity.clearChatMsg();
        }
    }
}
